package cn.yitun.chat.yitunserverfilecore.service.impl;


import cn.yitun.chat.yituncommonutil.YitunMD5Util;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.file.GroupHeadDataDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.file.UserHeadDataDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupHeadDataDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.UserHeadDataDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupHeadDataInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.userchat.UserHeadDataInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupHeadDataVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserHeadDataVo;
import cn.yitun.chat.yitunservercommonlib.util.FileNameUtil;
import cn.yitun.chat.yitunserverfilecore.component.AcceptFileHandler;
import cn.yitun.chat.yitunserverfilecore.component.data.FileInfo;
import cn.yitun.chat.yitunserverfilecore.dao.FileDataDao;
import cn.yitun.chat.yitunserverfilecore.manage.FileServerConfigManager;
import cn.yitun.chat.yitunserverfilecore.service.HeadService;
import cn.yitun.chat.yitunserverfilecore.util.FilePathUtil;
import cn.yitun.chat.yitunserverfilecore.util.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author yitun
 * @date 2019年12月30日 下午8:59:57
 * @version 1.1
 */
@Service
@Transactional
public class HeadServiceImpl implements HeadService {

    private String upload = "upload";
    @Autowired
    private FileDataDao commonDAO;
    @Resource
    private FileServerConfigManager fileServerConfigManager;


    AcceptFileHandler acceptFileHandler = new AcceptFileHandler();

    public List<UserHeadDataVo> userUploadByIO(HttpServletRequest request, Long userId) {
        String basePath = request.getSession().getServletContext().getRealPath("/") + upload;
        String path = fileServerConfigManager.getSavePath();
        StringBuilder rootPath = new StringBuilder((YitunStringUtil.isEmpty(path)) ? basePath : path);// 拼接文件上传目录
        String nodePath = FilePathUtil.getNodePath("head/user/");
        List<FileInfo> list = acceptFileHandler.uploadByIO(request, rootPath.toString(), nodePath);
        return addUserHead(list, userId);
    }

    private List<UserHeadDataVo> addUserHead(List<FileInfo> fileInfoList, Long userId) {
        List<UserHeadDataVo> list = new ArrayList<>();

        if (null != fileInfoList && !fileInfoList.isEmpty()) {
            for (FileInfo fi : fileInfoList) {
                File file = fi.getFile();
                long length = file.length();
                String rootPath = fi.getRootPath();
                String nodePath = fi.getNodePath();
                String saveName = fi.getSaveName();
                String name = fi.getName();
                String suffixName = FileNameUtil.getSuffixName(name);
                UserHeadDataInfo userHead = commonDAO.getUserHeadDataByUserId(userId);
                if (userHead != null)
                    commonDAO.removeUserHeadByUserId(userId);

                UserHeadDataDto fd = new UserHeadDataDto();
                fd.setName(name);
                fd.setSaveName(saveName);
                fd.setSuffixName(suffixName);
                fd.setUserId(userId);
                fd.setSize(length);
                fd.setRootPath(rootPath);
                fd.setNodePath(nodePath);
                fd.setUrl("/files/" + nodePath + saveName);
                fd.setMd5(YitunMD5Util.md5L32(saveName));
                fd.setType(String.valueOf(FileType.dealFileType(suffixName)));
                fd.setCreateBy(userId);
                fd.setUpdateBy(userId);
                commonDAO.saveUserHeadData(fd);

                list.add(UserHeadDataDto2VoConverter.INSTANCE.dto2vo(fd));
            }
        }
        return list;
    }

    public List<GroupHeadDataVo> groupUploadByIO(HttpServletRequest request, Long userId, Long groupId) {
        String basePath = request.getSession().getServletContext().getRealPath("/") + upload;
        String path = fileServerConfigManager.getSavePath();
        StringBuilder rootPath = new StringBuilder((YitunStringUtil.isEmpty(path)) ? basePath : path);// 拼接文件上传目录

        String nodePath = FilePathUtil.getNodePath("head/group/");
        List<FileInfo> list = acceptFileHandler.uploadByIO(request, rootPath.toString(), nodePath);
        return addGroupHead(list, userId, groupId);
    }

    private List<GroupHeadDataVo> addGroupHead(List<FileInfo> fileInfoList, Long userId, Long groupId) {
        List<GroupHeadDataVo> list = new ArrayList<>();

        if (null != fileInfoList && !fileInfoList.isEmpty()) {
            for (FileInfo fi : fileInfoList) {
                File file = fi.getFile();
                long length = file.length();
                String rootPath = fi.getRootPath();
                String nodePath = fi.getNodePath();
                String saveName = fi.getSaveName();
                String name = fi.getName();
                String suffixName = FileNameUtil.getSuffixName(name);

                if (commonDAO.countGroupHeadDataByGroupId(groupId) > 0)
                    commonDAO.removeGroupHeadByGroupId(groupId);
                GroupHeadDataDto fd = new GroupHeadDataDto();
                fd.setName(name);
                fd.setSaveName(saveName);
                fd.setSuffixName(suffixName);
                fd.setUserId(userId);
                fd.setGroupId(groupId);
                fd.setSize(length);
                fd.setRootPath(rootPath);
                fd.setNodePath(nodePath);
                fd.setUrl("/files/" + nodePath + saveName);
                fd.setMd5(YitunMD5Util.md5L32(saveName));
                fd.setType(String.valueOf(FileType.dealFileType(suffixName)));
                fd.setCreateBy(userId);
                fd.setUpdateBy(userId);
                commonDAO.saveGroupHeadData(fd);

                list.add(GroupHeadDataDto2VoConverter.INSTANCE.dto2vo(fd));
            }
        }
        return list;
    }

    public UserHeadDataInfo getUserHeadData(Long id) {
        return this.commonDAO.getUserHeadData(id);
    }

    public GroupHeadDataInfo getGroupHeadData(Long id) {
        return this.commonDAO.getGroupHeadData(id);
    }
}